package com.hadoop.util.address;

import com.google.gson.Gson;
import com.hadoop.util.JsonValidator;
import com.hadoop.util.Misc;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * AddressVo
 * <p>
 *     公共地址vo类
 * </p>
 *
 * @date 2017/10/11
 */
public class AddressVo {
    private Integer prov;        //省份
    private Integer city;        //城市
    private Integer dist;        //镇区
    private String address;      //详细地址(用户手动输入的地址细节，比如小区，楼号等）
    private String detail;       //拼装好的完整地址

    /**
     *  拼装完整地址
     * @param address
     * @return
     */
    public static AddressVo fillDetail(AddressVo address){
        if(null != address){
            String prov = OptionMap.getProvAddr(address.getProv());
            String city = OptionMap.getCityAddr(address.getCity());
            String town = OptionMap.getTownAddr(address.getDist());
            if(city.equalsIgnoreCase(town)){//选择了全市范围,即镇=市
                town = "";
            }
            String detail = prov + city + town + address.getAddress();
            address.setDetail(detail);
        }
        return address;
    }
    /**
     * addresVo的json转对象
     * @param json
     * @return
     */
    public static AddressVo toVo(String json){
        JsonValidator jsonValid = new JsonValidator();
        AddressVo address = new AddressVo();
        Gson gson = new Gson();
        if(jsonValid.validate(json))  {//住址信息赋值
            json = StringUtils.replace(json, "其他", ""); //仓库选择了"其它" ,不要显示
            address = gson.fromJson(json, AddressVo.class);
        } else {
            if("其他".equals(json) || "其它".equals(json)){
                address.setAddress("其它");
                address.setDetail("其它");
            } else {
                address.setDetail(json);
            }
        }
        return address;
    }

    /**
     * 拼装前端传过来的不按地址对象传值的数据,手动转为地址对象
     * @param addrCode 地址代码(镇区)
     * @param addrDesc 镇区之后的详细地区(挂牌时存的是仓库的地址)
     * @return
     */
    public static AddressVo buildAddrVoByCodeAndDesc(Integer addrCode,String addrDesc){
        AddressVo vo = new AddressVo();
        JsonValidator jsonValid = new JsonValidator();
        if(jsonValid.validate(addrDesc)){
            vo = toVo(addrDesc);
        }else{
            if (null != addrCode && StringUtils.isNotBlank(addrDesc)) {
                Integer prov = NumberUtils.toInt(StringUtils.left(addrCode+"", 2) + "000000");
                Integer city = NumberUtils.toInt(StringUtils.left(addrCode+"", 4) + "0000");
                vo.setProv(prov);
                vo.setCity(city);
                vo.setDist(addrCode);
                vo.setAddress(addrDesc);
                vo.setDetail(OptionMap.getFullAddr(addrCode) + addrDesc);
            }
        }

        return vo;
    }


    /**
     * 取得完整的中文地址
     * 例：{"prov":14000000,"city":14010000,"dist":14010003,"address":"高盛科技大厦"}
     * 返回：广东省东莞市南城区高盛科技大厦
     * @param addrJsonStr
     * @return
     */
    public static String getAddrStrByJson(String addrJsonStr) {
        if (StringUtils.isBlank(addrJsonStr)) {
            return "";
        }
        AddressVo address = toVo(addrJsonStr);
        return address.getDetail();
    }


    /**
     * 转换企业的旧地址格式为新格式JSON
     * @param oldAddr
     * @return
     */
    public static AddressVo getAddresVoByOldAddr(String oldAddr) {
        AddressVo addressVo = new AddressVo();
        if (StringUtils.isNotBlank(oldAddr)) {
            Integer prov = OptionMap.getCityCodeByAddr(oldAddr, OptionMap.AreaType.PROV);
            Integer city = OptionMap.getCityCodeByAddr(oldAddr, OptionMap.AreaType.CITY);
            Integer dist = OptionMap.getCityCodeByAddr(oldAddr, OptionMap.AreaType.TOWN);
            addressVo.setProv(prov);
            addressVo.setCity(city);
            addressVo.setDist(dist);
            // 由于处理特性转换相关值
            if(Misc.isNotZero(dist) && Misc.isNotZero(city) && dist.equals(city)) { dist = 0; }
            if(Misc.isNotZero(city) && Misc.isNotZero(prov) && city.equals(prov)) { city = 0; }
            // 旧地址按“省”、“市”、“区”/“镇”来截位获取详细信息
            String detail = "";  // 详细信息
            if(Misc.isZero(dist)){ // 如果“区”/“镇”不存在
                if(Misc.isZero(city)){ // 如果“市”不存在
                    if(Misc.isZero(prov)){ // 如果“省”不存在
                        detail = oldAddr;
                    }else{
                        detail = StringUtils.substringAfter(oldAddr, "省");
                    }
                }else{
                    detail = StringUtils.substringAfter(oldAddr, "市");
                }
            }else{
                detail = StringUtils.substringAfter(oldAddr, "区");
                if (StringUtils.isBlank(detail)) {
                    detail = StringUtils.substringAfterLast(oldAddr, "镇");
                }
            }
            /* 取出旧数据的街道地址
            oldAddr = StringUtils.substringAfterLast(oldAddr, "区");
            if (StringUtils.isBlank(oldAddr)) {
                oldAddr = StringUtils.substringAfterLast(oldAddr, "市");
            } */
            addressVo.setAddress(detail);
        }
        return addressVo;
    }

    /**
     * addresVo的对象转json
     * @param prov
     * @param city
     * @param dist
     * @param address
     * @return
     */
    public static String toJson(Integer prov,Integer city,Integer dist,String address){
        AddressVo obj = new AddressVo();
        Gson gson = new Gson();
        obj.setProv(prov);
        obj.setCity(city);
        obj.setDist(dist);
        obj.setAddress(address);
        return gson.toJson(obj);
    }

    /**
     * addresVo的对象转json
     * @param prov
     * @param city
     * @param dist
     * @param address
     * @param detail
     * @return
     */
    public static String toJson(Integer prov,Integer city,Integer dist,String address,String detail){
        AddressVo obj = new AddressVo();
        Gson gson = new Gson();
        obj.setProv(prov);
        obj.setCity(city);
        obj.setDist(dist);
        obj.setAddress(address);
        obj.setDetail(detail);
        return gson.toJson(obj);
    }

    public String getDetail() {
        if (StringUtils.isBlank(detail)) { //拼装完整地址
            fillDetail(this);
        }
        return detail;
    }


    public Integer getProv() {
        return prov;
    }

    public void setProv(Integer prov) {
        this.prov = prov;
    }

    public Integer getCity() {
        return city;
    }

    public void setCity(Integer city) {
        this.city = city;
    }

    public Integer getDist() {
        return dist;
    }

    public void setDist(Integer dist) {
        this.dist = dist;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }
}
